﻿using System.Drawing;
using System.Windows.Forms;

using Elifart.Library;

namespace Elifart.Editor.CategoryTabs.AssociationControls
{
    public class AssociationTreeNode : TreeNode
    {
        #region Constructor

        public AssociationTreeNode(Association footballAssociation, ImageList flagImageList)
        {
            this._flagImageList = flagImageList;
            this.Association = footballAssociation;
            this.Name = this.Association.GUID;
            this.Text = this.Association.Abbreviation;
            this.FlagImage = AssociationManager.GetManager().GetAssociationFlag(footballAssociation.FlagName);
        }

        #endregion

        #region Attributes and Properties

        private Association _association;
        public Association Association
        {
            get { return this._association; }
            private set 
            { 
                this._association = value;
                if (this._association != null)
                    this._association.Notifier.PropertyChanged += Association_PropertyChanged;
            }
        }

        public string AssociationName
        {
            get 
            {
                if (this.Association == null)
                    return null;
                return this.Association.Name; 
            }
        }

        public string AssociationAbbreviation
        {
            get 
            {
                if (this.Association == null)
                    return null;
                return this.Association.Abbreviation; 
            }
        }

        private Image _flagImage;
        public Image FlagImage
        {
            get { return this._flagImage; }
            set
            {
                if ((this.FlagImageList != null) &&
                    (this._flagImage != null))
                {
                    if (this.FlagImageList.Images.ContainsKey(this._flagImage.GetHashCode().ToString()))
                        this.FlagImageList.Images.RemoveByKey(this._flagImage.GetHashCode().ToString());
                }

                this._flagImage = value;

                if ((this.FlagImageList != null) &&
                    (this._flagImage != null))
                {
                    this.FlagImageList.Images.Add(this._flagImage.GetHashCode().ToString(), this._flagImage);
                    this.ImageKey = this._flagImage.GetHashCode().ToString();
                    this.SelectedImageKey = this._flagImage.GetHashCode().ToString();
                }
            }
        }

        private ImageList _flagImageList;
        public ImageList FlagImageList
        {
            get { return this._flagImageList; }
        }

        #endregion

        #region Signed Events Methods

        private void Association_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Association association = sender as Association;

            if (e.PropertyName == "GUID")
                this.Name = association.GUID.ToString();
            if (e.PropertyName == "Abbreviation")
                this.Text = association.Abbreviation;
            if (e.PropertyName == "FlagName")
                this.FlagImage = AssociationManager.GetManager().GetAssociationFlag(association.FlagName);
        }

        #endregion        
    }
}
